package com.quick.service;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @ClassName QuartzService
 * @Description TODO
 * @Author wangxin332
 * @Date 2018/10/1 下午9:15
 * @Version 1.0
 **/
@Service
public class QuartzService {
	@Autowired
	private Scheduler scheduler;

	/****
	 * 暂停一个任务
	 * @param triggerName
	 */
	public void pauseJob(String triggerName){
		try {
			JobKey jobKey = new JobKey(triggerName);
			JobDetail jobDetail = scheduler.getJobDetail(jobKey);
			if (jobDetail==null){
				return;
			}
			System.out.println("开始暂停一个定时器");
			scheduler.pauseJob(jobKey);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
	public void startJob(String triggerName){
		try {
			JobKey jobKey = new JobKey(triggerName);
			TriggerKey triggerKey = new TriggerKey(triggerName);
			JobDetail jobDetail = scheduler.getJobDetail(jobKey);
			Trigger trigger = scheduler.getTrigger(triggerKey);
			if (jobDetail==null){
				return;
			}
			scheduler.scheduleJob(trigger);
			scheduler.resumeJob(jobKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
